//
//  MemberSearchHeaderView.m
//  Findif
//
//  Created by Mac on 2019/2/27.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MemberSearchHeaderView.h"

@interface MemberSearchHeaderView ()<UITextFieldDelegate>

//@property (nonatomic, weak)UIButton * backButton;
//@property (nonatomic, weak)UIImageView * searchImageView;
//@property (nonatomic, weak)UILabel * searchLabel;
@property (nonatomic, weak)UITextField * textField;

@end

@implementation MemberSearchHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
        
        //监听当键将要退出时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    self.userInteractionEnabled = YES;
    
    
//    UIButton * backButton = [UIButton new];
//
//    backButton.backgroundColor = ZRWhiteColor;
//    backButton.layer.cornerRadius = 5 * SCREENH6;
//    backButton.layer.masksToBounds = YES;
//    backButton.layer.borderColor = [ColorWithRGB(0xe4e4e4) CGColor];
//    backButton.layer.borderWidth = 1.0f;
//
//    self.backButton= backButton;
//
//    [self addSubview:self.backButton];
//
//
//    UIImageView * searchImageView = [UIImageView new];
//
//    searchImageView.image = [UIImage imageNamed:@"搜索"];
//    searchImageView.userInteractionEnabled = NO;
//
//    self.searchImageView = searchImageView;
//
//    [self.backButton addSubview:self.searchImageView];
//
//
//    UILabel * searchLabel = [UILabel new];
//
//    searchLabel.text = @"搜索昵称";
//    searchLabel.textColor = ColorWithRGB(0x909090);
//    searchLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    searchLabel.userInteractionEnabled = NO;
//
//    self.searchLabel = searchLabel;
//
//    [self.backButton addSubview:self.searchLabel];
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
    //    textFiled.backgroundColor = [UIColor clearColor];
    //    textFiled.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xa5a5a5);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索昵称" attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 30)];
    //设置显示模式为永远显示(默认不显示)
    UIImageView * searchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, 6 * SCREENH6, 18 * SCREENH6, 18 * SCREENH6)];
    [leftView addSubview:searchImageView];
    textFiled.leftView = leftView;
    searchImageView.image = [UIImage imageNamed:@"搜索"];
    searchImageView.userInteractionEnabled = NO;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.returnKeyType = UIReturnKeyDone;
    textFiled.delegate = self;
    
    self.textField = textFiled;
    [self addSubview:self.textField];
}


- (void)layoutSubviews{
    
//    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
//        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self).offset(10 * SCREENH6);
//        make.height.mas_equalTo(30 * SCREENH6);
//    }];
//
//    [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(18 * SCREENH6);
//        make.leading.mas_equalTo(self.backButton).offset(10 * SCREENH6);
//        make.centerY.mas_equalTo(self.backButton);
//    }];
//
//    [self.searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.backButton);
//        make.leading.mas_equalTo(self.searchImageView.mas_trailing).offset(10 * SCREENH6);
//    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self).offset(10 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);

    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}


- (void)keyboardWillHide
{
    if (self.textField.text.length > 0) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(getSearchString:)])
        {
            
            [self.delegate getSearchString:self.textField.text];
        }
    }
    
}

@end
